package contents;

import contents.entities.DateSubstitutions;
import contents.entities.Subject;
import contents.entities.SubstitutionItem;
import contents.entities.Teacher;
import contents.entities.NoticeboardNotice;
import contents.entities.SubjectGrades;
import database.Database;
import database.DatabaseEntities;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class ParentContent {

    public static HttpServletRequest setContent(HttpServletRequest request) throws SQLException {
        String contentRequest = (String) request.getParameter("content");
        
        if (contentRequest == null) {
            // do nothing
        } else if (contentRequest.equals("noticeboard")) {
            request = setNoticeboardContent(request);
        } else if (contentRequest.equals("gradebook")) {
            request = setGradebookContent(request);
        } else if (contentRequest.equals("substitutions")) {
            request = setSubstitutionsContent(request);
        }
        
        return request;
    }
    
    private static HttpServletRequest setNoticeboardContent(HttpServletRequest request) throws SQLException {
        HttpSession session = request.getSession();
        int parentId = (Integer) session.getAttribute("userId");
        int studentId = Database.selectStudentForParent(parentId);
        int classId = Database.selectClassId(studentId);
        Map<Integer, NoticeboardNotice> notices = Database.selectNoticesForParent(classId);
        request.setAttribute("notices", notices.values());       
        
        return request;
    }
    
    private static HttpServletRequest setGradebookContent(HttpServletRequest request) throws SQLException {
        HttpSession session = request.getSession();
        int parentId = (Integer) session.getAttribute("userId");
        int studentId = Database.selectStudentForParent(parentId);
        Map<Integer, SubjectGrades> grades = Database.selectGradesForStudent(studentId);
        request.setAttribute("grades", grades.values());    
        
        return request;
    }
        
    private static HttpServletRequest setSubstitutionsContent(HttpServletRequest request) throws SQLException {
        Map<Integer, DateSubstitutions> dateSubstitutions = new HashMap<Integer, DateSubstitutions>();
        
        HttpSession session = request.getSession();
        int parentId = ((Integer) session.getAttribute("userId"));
        int studentId = Database.selectStudentForParent(parentId);
        int classId = Database.selectClassId(studentId);
        
        // get next monday date
        Date mondayDate = common.CommonMethods.getMondayDate();

        Date currentDate;
        for (int i = 0; i < 5; i++) {
            currentDate = new Date(mondayDate.getTime());
            currentDate.setDate(currentDate.getDate() + i);

            // get substitutions of the day and of student's class
            List<DatabaseEntities.Substitution> substitutions = Database.selectSubstitutionsOfDateAndClass(currentDate, classId);
            
            List<SubstitutionItem> substitutionItems = new ArrayList<SubstitutionItem>();
            
            for (int j = 0; j < substitutions.size(); j++) {
                DatabaseEntities.Substitution substitution = substitutions.get(j);
                
                String className = Database.selectClassName(substitution.classId);
                Integer lesson = Integer.valueOf(substitution.unit);
                Subject subject = Database.getSubject(substitution.subjectId);
                Teacher missingTeacher = Database.getTeacher(substitution.missingTeacherId);
                
                Teacher substitutingTeacher;
                if (substitution.substitutingTeacherId == null) {
                    substitutingTeacher = null;
                } else {
                    substitutingTeacher = Database.getTeacher(substitution.substitutingTeacherId.intValue());
                }
            
                substitutionItems.add(new SubstitutionItem(className, lesson, subject, missingTeacher, substitutingTeacher));
            }
            Collections.sort(substitutionItems, new SubstitutionItem.ByClassNameAndTimeComparator());
            
            dateSubstitutions.put(i, new DateSubstitutions(currentDate, substitutionItems));
        }
        
        request.setAttribute("substitutionsList", dateSubstitutions.values());
        return request;
    }
        
    private ParentContent() {
    }
}
